//
//  HWHomePageMainListVC.m
//  HappyWeekend
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWHomePageMainListVC.h"
#import "HWHomePageMainListTableViewCell.h"
#import "HWHomePageMenuView.h"
#import "HWHomePageMainModel.h"
#import "HWHomePageHeaderView.h"
#import "HWHeaderViewModel.h"
#import "HWHomePageSearchViewController.h"

@interface HWHomePageMainListVC ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *menuScrollView;

@property (nonatomic, strong) UITableView *handpickTableView;

@property (nonatomic, strong) UITableView *shoppingTabelView;

@property (nonatomic, strong) UITableView *travelTableView;

@property (nonatomic, strong) UITableView *courseTableView;

@property (nonatomic, strong) UITableView *foodTableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSMutableArray *headerViewArr;

@property (nonatomic, strong) HWHomePageMenuView *menuView;

@property (nonatomic, strong) UIView *channelView;

@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) NSMutableArray *deleteArray;


@end

@implementation HWHomePageMainListVC

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    self.navigationController.navigationBar.translucent = NO;
    
    _menuView = [[HWHomePageMenuView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 26)];
    _titleArray = [NSMutableArray arrayWithObjects:@"精选",@"周末逛店",@"体验课",@"周边游",@"尝美食", nil];
    for (int i = 0; i < _titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        btn.frame = CGRectMake(5 + i * 70, 5, 60, 16);
        //btn.backgroundColor = [UIColor greenColor];
        [btn setTitle:_titleArray[i] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:13];
        [_menuView.scrollView addSubview:btn];
    }
    [_menuView.pullDownButton addTarget:self action:@selector(pullDownButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    _menuView.scrollView.pagingEnabled = YES;
    
    [self.view addSubview:_menuView];
    
    [self addMenuScrollView];
    
    [self addTableViewWithOriginX:0 tag:5];
    
    
    [self addTableViewWithOriginX:kScreenWidth tag:12];
    [self addTableViewWithOriginX:kScreenWidth * 2 tag:13];
    [self addTableViewWithOriginX:kScreenWidth * 3 tag:14];
    [self addTableViewWithOriginX:kScreenWidth * 4 tag:15];
    
    [self setUpDataWith:@"5"];
    [self setUpDataWith:@"12"];
    [self setUpDataWith:@"13"];
    [self setUpDataWith:@"14"];
    [self setUpDataWith:@"15"];
    
    [self requestData];
    
    
    [self addChannelView];
    
    self.deleteArray = [NSMutableArray array];
    
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-sousuo-3"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBtnClick:)];
    self.navigationItem.rightBarButtonItem = rightBtn;

}

- (void)rightBtnClick:(UIBarButtonItem *)btn
{
    HWHomePageSearchViewController *searchVC = [[HWHomePageSearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)addChannelView
{
    self.channelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    UIButton *switchBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    switchBtn.frame = CGRectMake(10, 5, 60, 20);
    [switchBtn setTitle:@"切换频道" forState:(UIControlStateNormal)];
    [switchBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    switchBtn.tag = 123;
    switchBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.channelView addSubview:switchBtn];
    
    UIButton *sortBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    sortBtn.frame = CGRectMake(kScreenWidth - 120, 5, 70, 20);
    [sortBtn addTarget:self action:@selector(sortBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [sortBtn setTitle:@"排序或删除" forState:(UIControlStateNormal)];
    [sortBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    sortBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.channelView addSubview:sortBtn];
    
    UIButton *pullUpBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    pullUpBtn.frame = CGRectMake(kScreenWidth - 26, 5, 16, 16);
    [pullUpBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-shang-2"] forState:(UIControlStateNormal)];
    [pullUpBtn addTarget:self action:@selector(pullUpBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.channelView addSubview:pullUpBtn];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 30, kScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.3];
    [self.channelView addSubview:lineView];
    
    for (int i = 0; i < _titleArray.count; i++) {
        UILabel *btn = [[UILabel alloc] initWithFrame:CGRectMake(10 + (i % 4) * 90, 50 + (i / 4) * 40, 70, 20)];
        btn.text = _titleArray[i];
        btn.tag = 222 + i;
        btn.textAlignment = NSTextAlignmentCenter;
        btn.font = [UIFont systemFontOfSize:14];
        btn.layer.borderColor = [UIColor grayColor].CGColor;
        btn.layer.borderWidth = 1;
        //[btn setTitle:_titleArray[i] forState:(UIControlStateNormal)];
        //[btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        //btn.backgroundColor = [UIColor greenColor];
        //btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.channelView addSubview:btn];
    }
    self.channelView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.channelView];
    self.channelView.hidden = YES;
    
}


- (void)sortBtnClick:(UIButton *)btn
{
    UIButton *b = [self.channelView viewWithTag:123];
    NSString *str = [btn titleForState:(UIControlStateNormal)];
    if ([str isEqualToString:@"排序或删除"]) {
        [btn setTitle:@"完成" forState:(UIControlStateNormal)];
        [b setTitle:@"拖动排序" forState:(UIControlStateNormal)];
        for (int i = 0; i < self.titleArray.count; i++) {
            UILabel *label = (UILabel *)[self.channelView viewWithTag:222 + i];
            label.userInteractionEnabled = YES;
            if (i == 0) {
                label.textColor = [UIColor grayColor];
            } else {
                if (label.subviews.count == 0) {
                    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
                    button.frame = CGRectMake(label.frame.size.width - 8, -8, 16, 16);
                    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-quxiao"] forState:(UIControlStateNormal)];
                    [button addTarget:self action:@selector(cancelButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
                    button.layer.cornerRadius = 8;
                    button.layer.masksToBounds = YES;
                    [label addSubview:button];
                } else {
                    label.subviews.lastObject.hidden = NO;
                }
                
            }
        }
        
        
    } else {
        [btn setTitle:@"排序或删除" forState:(UIControlStateNormal)];
        [b setTitle:@"切换频道" forState:(UIControlStateNormal)];
        
        for (int i = 0; i < self.titleArray.count; i++) {
            UILabel *label = [(UILabel *)self.channelView viewWithTag:(222 + i)];
            if (i == 0) {
                label.textColor = [UIColor blackColor];
            } else {
                UIButton *button = label.subviews.lastObject;
                button.hidden = YES;
            }
        }
        
    }
    
}

- (void)cancelButtonClick:(UIButton *)button
{
    NSLog(@"点击");
    UILabel *label = (UILabel *)button.superview;
    CGRect temp = label.frame;
    temp.origin.x = 10 + (self.deleteArray.count % 4) * 90;
    temp.origin.y = 180 + (self.deleteArray.count / 4) * 40;
    label.frame = temp;
    label.subviews.lastObject.hidden = YES;
    [self.deleteArray addObject:label];
    [self.titleArray removeObject:label.text];
}



- (void)pullUpBtnClick:(UIButton *)btn
{
    [UIView animateWithDuration:1 animations:^{

        self.channelView.hidden = YES;
    }];
}


- (void)pullDownButtonClick:(UIButton *)btn
{
    [UIView animateWithDuration:1 animations:^{
        self.channelView.hidden = NO;
    }];
}


- (void)requestData
{
    [HWRequestManager requestWithUrl:@"http://api.guozhoumoapp.com/v2/banners" parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *bannersArray = dic[@"data"][@"banners"];
        self.headerViewArr = [NSMutableArray array];
        for (NSDictionary *aDic in bannersArray) {
            HWHeaderViewModel *model = [[HWHeaderViewModel alloc] init];
            [model setValuesForKeysWithDictionary:aDic];
            [self.headerViewArr addObject:model];
        }
        
        UITableView *tableView = (UITableView *)[_menuScrollView viewWithTag:5];
        HWHomePageHeaderView *headerView = [[HWHomePageHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth / 720 * 288)];
        headerView.scrollViewImageArray = self.headerViewArr;
        tableView.tableHeaderView = headerView;
        
    } failedWithError:^(NSError *connectionError) {
        
    }];
}



- (void)setUpDataWith:(NSString *)str
{
    [HWRequestManager requestWithUrl:[NSString stringWithFormat:@"http://api.guozhoumoapp.com/v2/channels/%@/items?limit=20&offset=0&gender=2&generation=1",str] parmDic:nil method:GET1 succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *itemsArray = dic[@"data"][@"items"];
        self.dataArray = [NSMutableArray array];
        for (NSDictionary *aDic in itemsArray) {
            HWHomePageMainModel *model = [[HWHomePageMainModel alloc] init];
            [model setValuesForKeysWithDictionary:aDic];
            [self.dataArray addObject:model];
        }
        
        NSInteger tag = [str integerValue];
        UITableView *tableView = [(UITableView *)_menuScrollView viewWithTag:tag];
        [tableView reloadData];
        
    } failedWithError:^(NSError *connectionError) {
        
    }];
}


- (void)addMenuScrollView
{
    _menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 26, kScreenWidth, kScreenHeight - 20 - 64)];
    _menuScrollView.pagingEnabled = YES;
    _menuScrollView.tag = 111;
    _menuScrollView.contentSize = CGSizeMake(kScreenWidth * 5, kScreenHeight - 20 - 64);
    _menuScrollView.delegate = self;
    [self.view addSubview:_menuScrollView];
    
}

- (void)addTableViewWithOriginX:(CGFloat)x tag:(CGFloat)tag
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(x, 0, kScreenWidth, kScreenHeight - 64 - 26 - 49) style:(UITableViewStylePlain)];
    tableView.tag = tag;
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.rowHeight = 30 + kScreenWidth / 600 * 275;
    [_menuScrollView addSubview:tableView];
    [tableView registerClass:[HWHomePageMainListTableViewCell class] forCellReuseIdentifier:@"HWHomePageMainListTableViewCell"];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWHomePageMainListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWHomePageMainListTableViewCell" forIndexPath:indexPath];
    HWHomePageMainModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 5) {
        cell.timeLabel.hidden = NO;
    } else {
        cell.timeLabel.hidden = YES;
    }
    return cell;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //NSLog(@"%f",scrollView.contentOffset.x);
    CGRect temp = _menuView.lineView.frame;
    temp.origin.x = (scrollView.contentOffset.x / (kScreenWidth * 5)) * (kScreenWidth - 26);
    _menuView.lineView.frame = temp;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
